#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define N 5

priority_queue<int, vector<int>, greater<int>> Queue;

int a[N] = {1, 2, 2, 5, 9};

void HuffmanCode()
{
    for (const int &i : a)
    {
        Queue.push(i);
    }

    int ans = 0;

    while (Queue.size() > 1)
    {
        int a = Queue.top();
        Queue.pop();
        int b = Queue.top();
        Queue.pop();
        Queue.push(a+b);
        ans += a+b;
    }

    cout << ans << endl;
}

int main()
{
    HuffmanCode();
    return 0;
}
